package com.shop.api;

import com.github.pagehelper.PageHelper;
import com.shop.service.ProductService;
import com.shop.util.Result;
import com.shop.util.ResultGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/goods")
public class GoodsApi {

    @Resource
    private ProductService productService;


    @GetMapping("/search")
    public Result getUserList(@RequestParam String q,
                              @RequestParam(defaultValue = "1") int pageNum,
                              @RequestParam(defaultValue = "10") int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List list = productService.searchProduct(q);
        if (list.size() != 0) {
            log.info("goodsApi,搜索了{}", q);
            return ResultGenerator.genSuccessResult(list);
        }
        return ResultGenerator.genFailResult(50060);
    }

    @GetMapping("/info/{productId}")
    public Result getInfo(@PathVariable("productId") Long productId) {
        Map result = productService.searchProductInfo(productId);
        if (result.size() != 0) {
            log.info("goodsApi,访问了{}", productId);
            return ResultGenerator.genSuccessResult(result);
        }
        return ResultGenerator.genFailResult(50061);
    }
}
